import QtQuick
import QtQuick.Controls

Window {
    visible: true
    width: 400
    height: 600
    title: "SwipeDelegate Demo"

    ListModel {
        id: listModel
        ListElement { name: "Item 1" }
        ListElement { name: "Item 2" }
        ListElement { name: "Item 3" }
        ListElement { name: "Item 4" }
        ListElement { name: "Item 5" }
    }

    ListView {
        id: listView
        anchors.fill: parent
        model: listModel
        delegate: SwipeDelegate {
            id: swipeDelegate
            text: name
            width: listView.width

            //向左滑动,显示delete
            swipe.right: Rectangle {
                width: parent.width
                height: parent.height
                color: "red"

                Label {
                    text: "Delete"
                    color: "white"
                    anchors.centerIn: parent
                }

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        if (seqAnimation.running) {
                            console.log("seqAnimation is running, can not remove this item ...")
                        } else {
                            listModel.remove(index)
                        }
                    }
                }
            }

            //向右滑动,显示archive
            swipe.left: Rectangle {
                width: parent.width
                height: parent.height
                color: "green"

                Label {
                    text: "Archive"
                    color: "white"
                    anchors.centerIn: parent
                }

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        console.log("Archived:", name)
                    }
                }
            }

            SequentialAnimation {
                id: seqAnimation
                PropertyAction {target: swipeDelegate;property: "ListView.delayRemove";value: true}
                NumberAnimation {target: swipeDelegate;property: "height";to: 0;duration: 250}
                PropertyAction {target: swipeDelegate;property: "ListView.delayRemove";value: false}
            }

            ListView.onRemove: {
                console.log("onRemove !")
                seqAnimation.start();
            }
        }
    }
}
